package com.jack.blog.dto;

import com.jack.blog.entity.Blog;
import com.jack.blog.utils.DateUtil;
import com.jack.blog.utils.UUIDUtil;
import com.jack.blog.vo.info.BlogInfo;
import com.jack.blog.vo.BlogVo;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Component
public class BlogDto extends BaseDto{

    private void checkBlogInfo(BlogInfo blogInfo)throws IllegalArgumentException{
        if (blogInfo.getTitle().length()>15)
            throw new IllegalArgumentException("博客标题长度超过15!");
        if (blogInfo.getBlogAbstract().length()>100)
            throw  new IllegalArgumentException("博客摘要长度超过100!");
        MultipartFile md = blogInfo.getMd();
        MultipartFile img = blogInfo.getImg();
        String type=getFileType(md.getOriginalFilename());
        if (!md.isEmpty()&&!allowBlog.contains(type))
            throw new IllegalArgumentException("文件类型 ( "+type+" ) 不支持!");
        type=getFileType(img.getOriginalFilename());
        if (!img.isEmpty()&&!allowBlogCover.contains(type))
            throw new IllegalArgumentException("文件类型 ( "+type+" ) 不支持!");
    }

    public Blog BlogUpdateToBlog(BlogInfo blogInfo){
        Blog blog=new Blog();
        checkBlogInfo(blogInfo);
        blog.setTitle(blogInfo.getTitle());
        blog.setBlogAbstract(blogInfo.getBlogAbstract());
        blog.setCid(blogInfo.getCid());
        if (!blogInfo.getImg().isEmpty())
            blog.setCover(UUIDUtil.getUUID()+".jpg");
        if (!blogInfo.getMd().isEmpty())
            blog.setHtml(UUIDUtil.getUUID()+".html");
        return blog;
    }
    public Blog BlogCreateToBlog(int uid, BlogInfo blogInfo)throws IllegalArgumentException{
        Blog blog=new Blog();
        checkBlogInfo(blogInfo);
        blog.setTitle(blogInfo.getTitle());
        blog.setBlogAbstract(blogInfo.getBlogAbstract());
        blog.setReleaseTime(DateUtil.getTimestamp());
        blog.setUid(uid);
        blog.setCid(blogInfo.getCid());
        if (blogInfo.getMd().isEmpty())
            throw new IllegalArgumentException("博客上传为空!");
        if (blogInfo.getImg().isEmpty())
            throw new IllegalArgumentException("封面上传为空!");
        blog.setCover(UUIDUtil.getUUID()+".jpg");
        blog.setHtml(UUIDUtil.getUUID()+".html");
        return blog;
    }

    public List<BlogVo> BlogToBlogVo(List<Blog> blogs){
        List<BlogVo> blogVos=new LinkedList<>();
        for (Blog blog:blogs)
            blogVos.add(BlogToBlogVo(blog));
        return blogVos;
    }

    public BlogVo BlogToBlogVo(Blog blog){
        BlogVo blogVo=new BlogVo();
        blogVo.setId(blog.getId());
        blogVo.setTitle(blog.getTitle());
        blogVo.setBlogAbstract(blog.getBlogAbstract());
        blogVo.setReleaseTime(DateUtil.getStringByTimestamp(blog.getReleaseTime(),"yyyy-MM-dd HH:mm"));
        blogVo.setBrowse(blog.getBrowse());
        blogVo.setGood(blog.getGood());
        blogVo.setBad(blog.getBad());
        blogVo.setMessage(blog.getMessage());
        blogVo.setCover(coverUrl + File.separator +blog.getCover());
        blogVo.setHtml(articleUrl+File.separator+blog.getHtml());
        blogVo.setCid(blog.getCid());
        blogVo.setUid(blog.getUid());
        return blogVo;
    }

    public Blog BlogVoToBlog(BlogVo blogVo){
        Blog blog=new Blog();
        blog.setId(blogVo.getId());
        blog.setBrowse(blogVo.getBrowse());
        blog.setGood(blogVo.getGood());
        blog.setBad(blogVo.getBad());
        blog.setMessage(blogVo.getMessage());
        return blog;
    }

    public Map<String,String> BlogVoToInfoCache(BlogVo blog){
        Map<String,String> map=new HashMap<>();
        map.put("id",String.valueOf(blog.getId()));
        map.put("title", blog.getTitle());
        map.put("blogAbstract", blog.getBlogAbstract());
        map.put("cid",String.valueOf(blog.getCid()));
        map.put("cover", blog.getCover());
        map.put("html",blog.getHtml());
        return map;
    }

    public Map<String,String> BlogVoToCache(BlogVo blog){
        Map<String,String> map=new HashMap<>();
        map.put("id",String.valueOf(blog.getId()));
        map.put("title", blog.getTitle());
        map.put("blogAbstract", blog.getBlogAbstract());
        map.put("releaseTime", blog.getReleaseTime());
        map.put("browse",String.valueOf(blog.getBrowse()));
        map.put("good",String.valueOf(blog.getGood()));
        map.put("bad",String.valueOf(blog.getBad()));
        map.put("message",String.valueOf(blog.getMessage()));
        map.put("uid",String.valueOf(blog.getUid()));
        map.put("cid",String.valueOf(blog.getCid()));
        map.put("cover", blog.getCover());
        map.put("html",blog.getHtml());
        return map;
    }

    public BlogVo CacheToBlogVo(Map<String,String> map){
        BlogVo blog=new BlogVo();
        blog.setId(Integer.valueOf(map.get("id")));
        blog.setTitle(map.get("title"));
        blog.setBlogAbstract(map.get("blogAbstract"));
        blog.setReleaseTime(map.get("releaseTime"));
        blog.setBrowse(Integer.valueOf(map.get("browse")));
        blog.setGood(Integer.valueOf(map.get("good")));
        blog.setBad(Integer.valueOf(map.get("bad")));
        blog.setMessage(Integer.valueOf(map.get("message")));
        blog.setUid(Integer.valueOf(map.get("uid")));
        blog.setCid(Integer.valueOf(map.get("cid")));
        blog.setCover(map.get("cover"));
        blog.setHtml(map.get("html"));
        return blog;
    }
}
